// pages/recipeDetail/recipeDetail.js
import Api from '../../utils/Api';
import Config from '../../utils/Config';
const _ = wx.cloud.database().command;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // 菜谱id
        _id: '',
        // 菜谱详细信息
        recipeDetail: '',
        // 关注该菜谱id的人数
        followPeople: 0,
        // 用户是否关注该菜谱
        isFollow: false,
        // 用户关注的id
        followId: 0,
        // 禁止点击
        unTap:false,
        state:false

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options) {
        wx.showNavigationBarLoading();
        // console.log(options);
        this.setData({
            _id: options._id
        });
        await this.getRecipeDetail();
        // let _id = options._id;
        // 访问喜+1
        // wx.cloud.callFunction({
        //     name:'updateById',
        //     data:{
        //         tableName:Config.tableName.recipesTable,
        //         _id:this.data._id,
        //         updateData:{
        //             views: _.inc(1)
        //         }
        //     },
        //     success:(res)=>{
        //         console.log(res)
        //     },
        //     fail:(err)=>{
        //         console.log(err)
        //     }
        // });
        this.data.recipeDetail.views++;
        this.setData({
            recipeDetail:this.data.recipeDetail
        })
        Api.updateById(Config.tableName.recipesTable, this.data._id, {
            views: _.inc(1)
        });
        // 获取关注该菜谱的人数
        let f_result = (await Api.getDataCount(Config.tableName.followTable, {
            recipeid: this.data._id
        }))
        this.setData({
            followPeople: f_result.total
        })
        // 判断当前用户是否关注了该菜谱
        // 1. 判断当前用户是否登陆
        let _openid = wx.getStorageSync('_openid');
        // 登陆则判断用户是否关注了该菜谱
        if (_openid) {
            let result = await Api.filterData(Config.tableName.followTable, {
                _openid,
                recipeid: this.data._id
            });
            if (result.data.length > 0) {
                this.setData({
                    isFollow: true,
                    followId: result.data[0]._id
                });
            }
        }
        // 获取当前用户_openid
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        this.setData({state:wx.getStorageSync('state')},()=>{
            if(this.data.state){
                this.onShareAppMessage = function(){
                        return {
                            title:this.data.recipeDetail.recipeName,
                            path:'/pages/recipeDetail/recipeDetail?_id='+this.data._id,
                            imageUrl:this.data.recipeDetail.files[0]
                        }
                }
            }
        });
        
    },
    async getRecipeDetail() {
        let recipeDetail = (await wx.cloud.callFunction({
            name: 'getLimitData',
            data: {
                tableName: Config.tableName.recipesTable,
                where: {
                    _id: this.data._id
                }
            }
        })).result.list[0];
        this.setData({
            recipeDetail
        });
        // console.log(recipeDetail);
        wx.hideNavigationBarLoading();
        wx.setNavigationBarTitle({
            title: recipeDetail.recipeName
        })
    },
    // 更改关注状态
    async changeFollow() {
        // 判断是否禁止点击
        if(!this.data.unTap){
            // 禁止继续点击
            this.setData({unTap:true})
        }else{
            wx.showToast({
              title: '不要连续点击!',
              icon:'none'
            })
            return;
        }
        // 判断是否登陆
        // 获取_openid
        let _openid = wx.getStorageSync('_openid');
        if (!_openid) {
            wx.showModal({
                cancelColor: 'cancelColor',
                title: '登陆提示',
                content: '你还没有登陆,是否前往登陆?',
                success:(res)=> {
                    if (res.confirm) {
                        wx.navigateTo({
                            url: '../index/index?page=2'
                        })
                    }else{
                        this.setData({unTap:false})
                    }
                }
            });
            return;
        }
        // 取消关注 
        if (this.data.isFollow) {
            let result = await Api.delById(Config.tableName.followTable, this.data.followId);
            if (result.stats.removed > 0) {
                // 关注数量-1
                Api.updateById(Config.tableName.recipesTable, this.data._id, {
                    follows: _.inc(-1)
                }).then(res => {
                    this.setData({
                        isFollow: false,
                        followId: 0,
                        followPeople:this.data.followPeople-1,
                        unTap:false
                    });
                    wx.showToast({
                        title: '已取消关注',
                        icon: 'none'
                    });
                });
                
            }
        }
        // 添加关注 
        else {
            let result = await Api.addData(Config.tableName.followTable, {
                recipeid: this.data._id,
                createDate: (new Date).getTime()
            })
            if (result._id) {
                // 关注数量+1
                Api.updateById(Config.tableName.recipesTable, this.data._id, {
                    follows: _.inc(1)
                }).then(res => {
                    this.setData({
                        isFollow: true,
                        followId: result._id,
                        followPeople:this.data.followPeople+1,
                        unTap:false
                    });
                    wx.showToast({
                        title: '关注成功',
                        icon: 'none'
                    });
                })

            }
        }
    },
    // 分享
    
})